# -*- encoding: utf-8 -*-
'''
本脚本由诺言Nuoyan制作，用于批量制作自定义方块
'''

import json
import os


beh_block_template = {
    "format_version": "1.10.0",
    "minecraft:block": {
        "description": {
            "identifier": "",
            "category": "items",
            "register_to_creative_menu": True
        },


        "components": {
            "minecraft:destroy_time": {
                "value": 5.0
            },
            "minecraft:block_light_emission": {
                "value": 0.0
            },
            "minecraft:explosion_resistance": {
                "value": 5.0
            },
            "minecraft:block_light_absorption": {
                "value": 0.0
            },
            "netease:aabb": {
                "collision": {
                    "min": [0, 0, 0],
                    "max": [1, 1, 1]
                },
                "clip": {
                    "min": [0, 0, 0],
                    "max": [1, 1, 1]
                }
            },
            "netease:render_layer": {
                "value": "alpha"
            },
            "netease:solid": {
                "value": False
            },
            "netease:pathable": {
                "value": True
            },
            "netease:tier": {
                "digger": "hatchet"
            },
            "netease:face_directional": {
                "type": "direction"
            }
        }
    }
}
box_name = {
    't31:example': "物品中文",
}


root = __file__[:__file__.rindex("scripts")]


with open(root + r"plugins\t31PixelBreakout_Game\resource_packs\t31PixelBreakoutGameRes\blocks.json", "r") as f:
    block_json = json.load(f)
with open(root + r"plugins\t31PixelBreakout_Game\resource_packs\t31PixelBreakoutGameRes\textures\terrain_texture.json", "r") as f:
    terrain_texture = json.load(f)


geo_path = root + r"plugins\t31PixelBreakout_Game\resource_packs\t31PixelBreakoutGameRes\models\netease_block"
lang = ""
for r, dirs, files in os.walk(geo_path):
    for fn in files:
        # fp = r + "\\" + fn
        # with open(fp, "r") as f:
        #     data = json.load(f)
        # identifier = "t31:" + data['minecraft:geometry'][0]['description']['identifier'].split(".")[1]
        # netease_geo = {
        #     "format_version": "1.13.0",
        #     "netease:block_geometry": {
        #         "description": {
        #             "identifier": identifier,
        #             "textures": [identifier],
        #             "item_texture": identifier + "_icon",
        #             "use_ao": True
        #         },
        #         "bones": data['minecraft:geometry'][0]['bones']
        #     }
        # }
        # with open(fp, "w") as f:
        #     json.dump(netease_geo, f, indent=4, separators=(", ", ": "), sort_keys=True)

        identifier = "t31:" + fn.split(".")[0]
        beh_block_template['minecraft:block']['description']['identifier'] = identifier
        with open(root + r"plugins\t31PixelBreakout_Game\behavior_packs\t31PixelBreakoutGameBeh\netease_blocks\%s" % fn.replace(".geo", ""), "w") as f:
            json.dump(beh_block_template, f, indent=4, separators=(", ", ": "), sort_keys=True)

        block_json[identifier] = {
            'netease_model': identifier,
            'sound': "wood"
        }

        terrain_texture['texture_data'][identifier] = {
            "textures": "textures/blocks/%s" % fn.split(".")[0],
        }
        terrain_texture['texture_data'][identifier + "_icon"] = {
            "textures": "textures/blocks/%s_icon" % fn.split(".")[0],
        }

        lang += "tile.%s.name=%s\n" % (identifier, box_name[identifier])


with open(root + r"plugins\t31PixelBreakout_Game\resource_packs\t31PixelBreakoutGameRes\blocks.json", "w") as f:
    json.dump(block_json, f, indent=4, separators=(", ", ": "), sort_keys=True)
with open(root + r"plugins\t31PixelBreakout_Game\resource_packs\t31PixelBreakoutGameRes\textures\terrain_texture.json", "w") as f:
    json.dump(terrain_texture, f, indent=4, separators=(", ", ": "), sort_keys=True)
with open(root + r"plugins\t31PixelBreakout_Game\resource_packs\texts\zh_CN.lang", "w") as f:
    f.write(lang)


















